package com.unitop.partner.common.otms;



import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;

/**
 * Jaxb2工具类
 * 
 * @author zhuc
 * @create 2013-3-29 下午2:40:14
 */
public class OTMSJaxbXmlConverter {

    /**
     * JavaBean转换成xml 默认编码UTF-8
     * 
     * @param obj
     * @param writer
     * @return
     */
    public static String convertToXml(Object obj) {
        return convertToXml(obj, "UTF-8", false);
    }

    /**
     * JavaBean转换成xml
     * 
     * @param obj
     * @param encoding
     * @return
     */
    public static String convertToXml(Object obj, String encoding, boolean formattedOutput) {
        String result = null;
        try {
            result = convertToXmlInternal(obj, encoding, formattedOutput);
        } catch (JAXBException je) {
            JAXBContextFactory.removeJAXBContext(obj.getClass());
            try {
                result = convertToXmlInternal(obj, encoding, formattedOutput);
            } catch (JAXBException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * JavaBean转换成xml
     * 
     * @param obj
     * @param encoding
     * @return
     * @throws JAXBException 
     */
    private static String convertToXmlInternal(Object obj, String encoding, boolean formattedOutput)
            throws JAXBException {
        JAXBContext context = JAXBContextFactory.getJAXBContext(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);

        return writer.toString();
    }
    
    /**
     * xml转换成JavaBean
     * 
     * @param xml
     * @param c
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T convertToJavaBean(String xml, Class<T> c) {
        try {
            Object obj = convertToJavaBeanInternal(xml, c);
            if (obj != null && obj.getClass() != c) {
                JAXBContextFactory.removeJAXBContext(c);
                obj = convertToJavaBeanInternal(xml, c);
            }
            return (T) obj;
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * xml转换成JavaBean
     * 
     * @param xml
     * @param c
     * @return
     */
    private static  Object convertToJavaBeanInternal(String xml, Class<?> c) throws JAXBException {
        JAXBContext context = JAXBContextFactory.getJAXBContext(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return unmarshaller.unmarshal(new StringReader(xml));
    }
}
